﻿using BBICMS.Infrastructure.UI;
using BBICMS.Model.ViewModel.Front;
using BBICMS.Repository;
using System;
using System.Linq;
using System.Web.Mvc;

namespace BBICMS.WebUI.Controllers
{
    public class NewsController : BaseController
    {
        public int PageSize = 16;
              
        public ActionResult Index(int page = 1)
        {
            var defaultLang = LanguageHelper.GetCurrentCulture();
            ArticlesVM bivm = new ArticlesVM
            {
               //Blogs = _unit.Blogs.GetAll().ToList(),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = _unit.Posts.GetAll().Count(p => p.Active && p.Lang == defaultLang)
                },
                 Posts = _unit.Posts.GetAll().Where(p=>p.Active && p.Lang == defaultLang).OrderByDescending(p => p.Id)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),

               

            };

             return View(bivm);
        }


        public PartialViewResult LasterNews()
        {
            var news = _unit.Posts.GetAll().Where(p => p.Active).OrderByDescending(p => p.Id).Take(2).ToList();
            return PartialView("_LasterNews", news);
        }


        //public ActionResult Articles(int id, int page = 1)
        //{
            
        //    ArticlesVM bivm = new ArticlesVM
        //    {
        //        Blogs = _unit.Blogs.GetAll().ToList(),
        //        PagingInfo = new PagingInfo
        //        {
        //            CurrentPage = page,
        //            ItemsPerPage = PageSize,
        //            TotalItems = _unit.Posts.GetAll().Count(p => p.BlogId==id)
        //        },
        //        Posts = _unit.Posts.GetAll().Where(p => p.BlogId == id).OrderByDescending(p => p.Id)
        //       .Skip((page - 1) * PageSize)
        //       .Take(PageSize)    ,
        //       Article = _unit.Posts.GetAll().OrderByDescending(p => p.AddedDate).FirstOrDefault()
        //    };
        //    return View("Index",bivm);
        //}

        public PartialViewResult _BlogList()
        {
            var lBlogs = _unit.Blogs.GetAll().ToList();
            return  PartialView("_BlogList",lBlogs);
        }


        public ActionResult Detail(int id)
        {
            ArticleDetailsVM dvm = new ArticleDetailsVM();
            var vPost = _unit.Posts.GetById(id);
            vPost.ViewCount += 1;
            _unit.Posts.Update(vPost);
            _unit.SaveChanges();
            
            dvm.Post = vPost;

            //dvm.Blogs = _unit.Blogs.GetAll().ToList();
            dvm.Article = _unit.Posts.GetAll().OrderByDescending(p => p.AddedDate).FirstOrDefault();

            var cu = LanguageHelper.GetCurrentCulture();

            var prev = _unit.Posts.GetAll().Where(s => s.Active && s.Id < id &&  s.Lang.Equals(cu, StringComparison.InvariantCultureIgnoreCase)).OrderByDescending(s => s.Id).FirstOrDefault();
            if (prev != null)
            {
                ViewBag.Prev = prev.Id;
            }

            var next = _unit.Posts.GetAll().Where(s => s.Active && s.Id > id &&  s.Lang.Equals(cu, StringComparison.InvariantCultureIgnoreCase)).OrderBy(s => s.Id).FirstOrDefault();
            if (next != null)
            {
                ViewBag.Next = next.Id;
            }
            return View(dvm);
        }

    }
}
